import Vue from 'vue'
import Vuex,{Store} from 'vuex'
import axios from 'axios'
import {v4} from 'uuid'

Vue.use(Vuex)

export default new Store({
  actions:{
    addPerson({state,commit},value){
      if(state.personList.length <= 10){
        commit('ADD_PERSON',value)
      }
    },
    getRandomImg({commit}){
      axios.get('https://dog.ceo/api/breed/eskimo/images/random').then(
        response => {
          const obj = {id:v4(),url:response.data.message}
          commit('SAVE_IMG',obj)
        },
        error => {
          alert(error.message)
        }
      )
    },
    getGoodImg({commit}){
      axios.get('https://api.uomg.com/api/rand.img3?sort=%E8%83%96%E6%AC%A1%E7%8C%AB&format=json').then(
        response => {
          const obj = {id:v4(),url:response.data.imgurl}
          commit('SAVE_IMG',obj)
        },
        error => {
          alert(error.message)
        }
      )
    }
  },
  mutations:{
    ADD_PERSON(state,value){
      state.personList.unshift(value)
    },
    SAVE_IMG(state,value){
      state.imgList.unshift(value)
    }
  },
  state:{
    personList:[
      {id:'asdfsay01',name:'老段',age:18},
      {id:'asdfsay02',name:'马冬',age:19},
      {id:'asdfsay03',name:'如花',age:69}
    ],
    imgList:[
      {id:'asdgytsaf01',url:'http://49.232.112.44/images/hot.jpg'},
      {id:'asdgytsaf02',url:'http://49.232.112.44/images/cool.jpg'}
    ]
  }
})